<?php
namespace Home\Controller;

class IndexController extends BaseController {
// class IndexController extends Controller {
    public function index(){
    	if (!isset($_SESSION ['mdd_user'])) {
    		redirect(U('/User/bind'));
    	}
    	
//     	$s = 'a:4:{s:9:"signature";s:40:"e4049700b3401749f3eccf30e52016ce5236d2ee";s:7:"echostr";s:19:"7040114306447927289";s:9:"timestamp";s:10:"1442399608";s:5:"nonce";s:7:"9322731";}';

//     	$data = unserialize($s);
    	
//     	dump($data);die;
    }
    
    public function showOpenId() {
    	dump($this->getOpenId());die;
    }
    
    /**
     * 加载美容师
     */
    public function ajaxLoadBeautician() {
    	$province = I('get.province');
    	$city = I('get.city');
    	 
    	$Model = D ( 'Beautician' );
    	
    	$lat = I('get.lat');
    	$lng = I('get.lng');
    	
    	if ($lat && $lng) {
    		$sql = 'select b.*,emp.status as estatus,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(('.$lat.
    		'*PI()/180-bp.latitude*PI()/180)/2),2)+COS('.$lat.'*PI()/180)*COS(bp.latitude*PI()/180)*POW(SIN(('.$lng.
    		'*PI()/180-bp.longitude*PI()/180)/2),2)))*1000) as distance from '.C('DB_PREFIX').'beauty_parlor as bp '.
    		'LEFT JOIN '.C('DB_PREFIX').'beauty_parlor_employe as emp on bp.id=emp.bp_id '.
    		'LEFT JOIN '.C('DB_PREFIX').'beautician as b on b.id=emp.beautician_id '.
    		' where bp.province="'.$province.'" and bp.city="'.$city
    		.'" AND b.province="'.$province.'" and b.city="'.$city
    		.'" and emp.status=1 AND b.status=1 order by distance';
    	
    		$list = array();
    		$ids = array();
    		
    		$beauticianList = $Model->query($sql);
    		foreach ($beauticianList as $key => $value) {
    			if (!$ids || !in_array($value ['id'], $ids)) {
    				$ids [] = $value ['id'];
    			} else {
    				unset($beauticianList [$key]);
    			}
    		}
    		
    		$list = $beauticianList;
    		foreach ( $list as $key => &$value ) {
    			if ($value ['professional_score'] || $value ['punctuality_score'] || $value ['communication_score']) {
    				$value ['start'] = ceil(($value ['professional_score'] + $value ['punctuality_score'] + $value ['communication_score'])/3/$value ['comment_total']);
    			} else {
    				$value ['start'] = 0;
    			}
    		
    			$list [$key] ['mathDis'] = $d = $value ['distance']/1000;
    			if ($d < 1) {
    				$list[$key] ['distance'] = round($d * 1000).'m';
    			} else {
    				$list[$key] ['distance'] = round($d, 1).'km';
    			}
    		}
    	} else {
    		$list = $Model->where(array('province' => $province, 'city' => $city, 'status' => 1))->select ();
    		if ($list) {
    			$list = $this->formatAddressDistance($list);
    		}
    	}
    	
    	$this->ajaxReturn(array('status' => 1, 'data' => $list));
    }
    
    /**
     * 美容师页面初始化
     */
    public function beautician() {
    	$latitude = I ('get.latitude');
    	$longitude = I ('get.longitude');
    	$address = I ('get.address');
    	 
    	if(I('get.')) {
    		$this->assign('latitude', $latitude);
    		$this->assign('longitude', $longitude);
    		$this->assign('address', $address);
    	}
    	 
    	$this->assign('select', 'Beautician');
    	$this->display ();
    }
    
    /**
     * 地址搜索页面初始化
     */
    public function customAddress() {
    	if (!IS_POST) {
    		$type = I('get.type');
    		
    		if ($type == 'beauty') {
    			$redirect = U('/BeautyParlor/index');
    		} else {
    			$redirect = U('/Index/beautician');
    		}
    		
    		$this->assign('redirect', $redirect);
    		$this->display();
    	}
    }
    
    /**
     * 美容师详情
     */
    public function detail() {
    	$id = I ( 'get.id' );
    	$Model = D ( 'Beautician' );
    
    	$item = $Model->getBeauticianInfo ( $id );
    	$PhotoModel = D ( 'Photo' );
    	$photos = $PhotoModel->getTargetPhotos ( $id );
    
    	$jobTitleDetails = M ( 'job_title' )->select ();
    	$jobTitles = array ();
    	foreach ( $jobTitleDetails as $key => $value ) {
    		$jobTitles [$value ['id']] = $value ['title'];
    	}
    
    	$this->assign ( 'jobTitles', $jobTitles );
    	$this->assign ( 'photos', $photos );
    
    	$experiences = D ( 'WorkExperience' )->getTargetExperiences ( $id );
    	$this->assign ( 'experiences', $experiences );
    
    	$ServiceItemModel = D ( 'ServiceItem' );
    	$serviceList = $ServiceItemModel->getServiceList ();
    	
    	$this->assign ( 'serviceList',  $serviceList);
    
    	$item ['professional_score'] = number_format($item ['professional_score']/$item ['comment_total'], 1,'.','');
    	$item ['punctuality_score'] = number_format($item ['punctuality_score']/$item ['comment_total'], 1,'.','');
    	$item ['communication_score'] = number_format($item ['communication_score']/$item ['comment_total'], 1,'.','');
    	
    	$start = ($item ['professional_score'] + $item ['punctuality_score'] + $item ['communication_score'])/3;
    	$item ['start'] = ceil($start);
    	$BeautyParlorModel = D('BeautyParlor');
    	$beauty = $BeautyParlorModel->getBestBeautyOfBeautician($id);
    	if ($beauty ['professional_score'] || $beauty ['punctuality_score'] || $beauty ['communication_score']) {
    		$beauty ['start'] = ceil(($beauty ['professional_score'] + $beauty ['punctuality_score'] + $beauty ['communication_score'])/3/$beauty ['comment_total']);
    	} else {
    		$beauty ['start'] = 0;
    	}
    	$total = $BeautyParlorModel->totalBeautyUser($beauty ['id']);
    	$beauty ['user_total'] = count($total);
    	$this->assign ('beauty', $beauty);
    	$this->assign ( 'item', $item );
    	$this->display ();
    }
    
    /**
     * @deprecated
     */
    public function getRound() {
    	$latitude = $lat;
    	$longitude = $lon;
    	 
    	$degree = (24901 * 1609) / 360.0;
    	$raidusMile = $raidus;
    	 
    	$dpmLat = 1 / $degree;
    	$radiusLat = $dpmLat * $raidusMile;
    	$minLat = $latitude - $radiusLat;
    	$maxLat = $latitude + $radiusLat;
    	 
    	$mpdLng = Math.abs($degree * Math.cos($latitude * (PI / 180)));
    	$dpmLng = 1 / $mpdLng;
    	$radiusLng = $dpmLng * $raidusMile;
    	$minLng = $longitude - $radiusLng;
    	$maxLng = $longitude + $radiusLng;
    }
    
    /**
     * 获取美容师的空闲时间
     */
    public function getTime() {
    	$beauticianId = I('get.beautician_id');
    	
    	$todayDate = date ( 'Y-m-d' );
    	$dayStart = strtotime ( $todayDate . ' 00:00:00' );
    	$dayEnd = strtotime ( $todayDate . ' 23:59:59' );
    	$todayStart = strtotime ( $todayDate . ' 08:00:00' );
    	$todayEnd = strtotime ( $todayDate . ' 22:00:00' );
    	
    	$ScheduleModel = M('schedule');
    	$setting = $ScheduleModel->where(array('uid' => $beauticianId))->order('CurrentDate DESC')
    	->limit(1)->find();
    	if ($setting) {
    		$setting = unserialize($setting ['workTime']);
    	}
    	
    	if (!$setting) {
    		$todaySetting = 0;
    		$tomorrowSetting = 0;
    		$afterTomorrowSetting = 0;
    	} else {
    		if (array_key_exists($dayStart, $setting)) {
				$todaySetting = $setting [$dayStart];
				if ($todaySetting == 0) {
					$todaySetting = array();
				}
			} else {
				$todaySetting = 0;
			}
			
			if (array_key_exists(strtotime ( '+1 day', $dayStart), $setting)) {
				$tomorrowSetting = $setting [strtotime ( '+1 day', $dayStart)];
				if ($tomorrowSetting == 0) {
					$tomorrowSetting = array();
				}
			} else {
				$tomorrowSetting = 0;
			}
			
			if (array_key_exists(strtotime ( '+2 day', $dayStart), $setting)) {
				$afterTomorrowSetting = $setting [strtotime ( '+2 day', $dayStart)];
				if ($afterTomorrowSetting == 0) {
					$afterTomorrowSetting = array();
				}
			} else {
				$afterTomorrowSetting = 0;
			}
    	}
    	
    	$data = array (
    		'today' => array (
    			'start' => $todayStart,
    			'end' => $todayEnd,
    			'setting' => $todaySetting
    		),
    		'tomorrow' => array (
    			'start' => strtotime ( '+1 day', $todayStart ),
    			'end' => strtotime ( '+1 day', $todayEnd ),
    			'setting' => $tomorrowSetting
    		),
    		'afterTomorrow' => array (
    			'start' => strtotime ( '+2 day', $todayStart ),
    			'end' => strtotime ( '+2 day', $todayEnd ),
    			'setting' => $afterTomorrowSetting
    		),
    		'config_start' => $todayStart,
    		'config_end' => $todayEnd
    	);
    	
    	$OrderModel = D('Order');
    	$orderTimes = array();
    	$orders= $OrderModel->getBeauticianOrderTimes($beauticianId, $todayStart);
    	if ($orders) {
    		foreach ($orders as $key => $order) {
    			$orderTimes [$order ['order_time']] = $order ['order_time'] + $order ['service_time'] * 60;
    		}
    	}
    	
    	$this->ajaxReturn (array('status' => 1, 'data' => array('time' => $data, 'orderTimes' => $orderTimes)));
    }
    
    public function gpsChange() {
    	$result = weixinToBaidu(I('get.lngAndLat'));
    	$this->ajaxReturn(array('status' => 1, 'data' => $result));
    }
    
    /**
     * 地址搜索
     * @return unknown
     */
    public function searchAddress() {
    	$query = I('get.query');
    	$location = I('get.location');
    	$city = I ('get.city');
    	
    	$result = baiduSuggestion($query, $location, $city);
    	 
    	if ($result) {
    		foreach ($result as $key => &$value) {
    			if ($value ['city'] == $city) {
    				$value ['value'] = $value ['name'];
    			} else {
    				unset($result [$key]);
    			}
    		}
    	}
    	
    	$data = array(
    		'query' => $query,
    		'suggestions' =>  $result?$result:array()
    	);
    	$this->ajaxReturn($data);
    }
    
    /**
     * 美容师选择页面
     */
    public function selectBeautician() {
    	$serviceId = I ('get.id');
    	$beautyId = I('get.beauty_id');
    		
    	$beauticianModel = D('Beautician');
    	 
    	$list = $beauticianModel->getBeautyBeautician($beautyId);
    	foreach ($list as $key => &$value) {
    		if ($value ['professional_score'] || $value ['punctuality_score'] || $value ['communication_score']) {
    			$value ['start'] = ceil(($value ['professional_score'] + $value ['punctuality_score'] + $value ['communication_score'])/3/$value ['comment_total']);
    		} else {
    			$value ['start'] = 0;
    		}
    	}
    	 
    	$this->assign('beautician_id', I('get.beautician_id'));
    	$this->assign('beauty_id', $beautyId);
    	$this->assign('beauty', D('BeautyParlor')->getBeautyInfo($beautyId));
    	$this->assign('serviceId', $serviceId);
    	$this->assign('list', $list);
    	$this->display();
    }
    
    /**
     * 美容师服务列表
     */
    public function selectBeauticianService() {
    	$beauticianId = I('get.id');
    	
    	$item = D ( 'Beautician' )->getBeauticianInfo ( $beauticianId );
    	$ServiceItemModel = D('ServiceItem');
    	 
    	$item ['start'] = ceil(($item ['professional_score'] + $item ['punctuality_score'] + $item ['communication_score'])/3/$item ['comment_total']);
    	$this->assign('beautician', $item);
    	$this->assign('lists', $ServiceItemModel->getServiceList());
    	 
    	$this->assign('hasMore', false);
    	$this->display();
    }
    
}